#!usr/bin/env python
# -*- coding:utf-8 _*-
"""
@author:xaoyaoyao
@file: tools.py
@time: 2018/08/19
"""

import time
import re
import sys
import tldextract


class Tools(object):
    def get_current_time(format_="[%Y-%m-%d %H:%M:%S]"):
        return time.strftime(format_, time.localtime(time.time()))

    def title_strip(title=None):
        if title is None or title == '':
            return None
        title = str(title).replace("\n", "").strip()
        title = re.sub(r'\w+满分作文[\w]+[--—―?：:]+', '', str(title))
        title = re.sub(r'\w+满分作文--', '', str(title))
        title = re.sub(r'\w+优秀作文：', '', str(title))
        title = re.sub(r'\w+作文题目：', '', str(title))
        title = re.sub(r'\w+高分作文：', '', str(title))
        title = re.sub(r'\w+[作范]文[：:]+', '', str(title))
        title = re.sub(r'\w+作文[—―?：:]+', '', str(title))
        title = re.sub(r'\w+赏析[—―?：:]+', '', str(title))
        title = re.sub(r'\w+欣赏[—―?：:]+', '', str(title))
        title = re.sub(r'\w+赏评[—―?：:]+', '', str(title))
        title = re.sub(r'\w+作文点评——', '', str(title))
        title = re.sub(r'\w+佳作[：:]+', '', str(title))
        title = re.sub(r'\w+高分作文：', '', str(title))
        title = re.sub(r'\w+作文系列\w+―', '', str(title))
        title = re.sub(r'\w+作文（全国卷）[—―?：:]+', '', str(title))
        title = re.sub(r'\S+】[—―?：:]+', '', str(title))
        title = re.sub(r'\w+题预测[—―?：:]+', '', str(title))
        title = re.sub(r'\w+经典素材[—―?：:]+', '', str(title))
        title = re.sub(r'\w+素材集锦[—―?：:]+', '', str(title))
        title = re.sub(r'\w+冲刺[—―?：:]+', '', str(title))
        title = re.sub(r'\w+大主题：[—―?：:]+', '', str(title))
        title = re.sub(r'_\d+字$', '', str(title))
        title = re.sub(r'\w+写作指导[—―?：:]+', '', str(title))
        title = re.sub(r'\w+作文题目[—―?：:]+', '', str(title))
        title = re.sub(r'\w+提分技法[—―?：:]+', '', str(title))
        title = re.sub(r'\w+点评[—―?：:]+', '', str(title))
        title = re.sub(r'\w+写作素材[—―?：:]+', '', str(title))
        title = re.sub(r'\w+话题\d+[—―?：:]+', '', str(title))
        title = re.sub(r'\w+素材[—―?：:]+', '', str(title))
        title = re.sub(r'\w+预测[—―?：:]+', '', str(title))
        title = re.sub(r'\w+（小考范文）', '', str(title))
        title = re.sub(r'\w+（小考满分作文）', '', str(title))
        title = re.sub(r'\w+（小考优秀范文）', '', str(title))
        title = re.sub(r'\w+作文题[—―?：:]+', '', str(title))
        title = re.sub(r'\w+满分作文', '', str(title))
        return title

    def content_strip(content=None):
        if content is None or content == '':
            return None
        content = str(content).replace("\n", "").strip()
        content = content.replace("\r", "").replace("\t", "").strip()
        content = re.sub(r'欢迎指教！\S+', '', str(content))
        content = re.sub(r'版权作品，未经\S+', '', str(content))
        content = re.sub(r'中国散文网首发\S+', '', str(content))
        return content

    def content_filter(content=None):
        if content is None or content == '':
            return None
        content = str(content).strip()
        content = re.sub(r'欢迎指教！\S+', '', str(content))
        content = re.sub(r'版权作品，未经\S+', '', str(content))
        content = re.sub(r'中国散文网首发\S+', '', str(content))
        content = re.sub(r'头图来源：视觉中国', '', str(content))
        content = re.sub(r'（来源：[\w)）]+', '', str(content))
        return content

    def author_filter(author=None):
        if author is None or author == '':
            return None
        author = str(author).replace("\n", "").strip()
        author = re.sub(r'作者:', '', str(author))
        return author

    def domain(url=None):
        if url is None or url == '':
            return None
        try:
            o = tldextract.extract(url)
            domain = "{}.{}".format(o.domain, o.suffix)
            return domain
        except Exception as e:
            return None

    def clean_html(html_text):
        if html_text is None or html_text == '':
            return None
        dr = re.compile(r'<[^>]+>', re.S)
        result = dr.sub('', html_text)
        return result
